package exam.axhu.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import exam.axhu.entity.Judge;
import exam.axhu.service.JudgeService;


@SuppressWarnings("serial")
@Controller @Scope("prototype")
public class JudgeAction extends ActionSupport{
//defaultClass: judgeLogin
	@Resource JudgeService judgeService;

	private Judge judge;
	public Judge getJudge() {
		return judge;
	}
	public void setJudge(Judge judge) {
		this.judge = judge;
	}
	
	
	public String editJudgeA(){
		ActionContext.getContext().put("editjudge", judgeService.findByID(judge.getJudgeID()));
		return "Edit";
	}
	

	public String editJudgeB(){
		Judge newjudge = judgeService.findByID(judge.getJudgeID());
		newjudge.setJudgeA(judge.getJudgeA());
		newjudge.setJudgeB(judge.getJudgeB());
		newjudge.setJudgeContent(judge.getJudgeContent());
		newjudge.setJudgeAnswer(judge.getJudgeAnswer());
		judgeService.update(newjudge);
		addActionMessage("修改成功!欢迎!");
		ActionContext.getContext().put("judges", judgeService.list());
		return "EditSucceed";
	}
	
	public String editJudges(){
		ActionContext.getContext().put("judges", judgeService.list());
		return "JudgeInfo";
	}

	
	public String AddJudgeA(){
		return "AddJudge";
	}
	
	public String AddJudgeB(){
	    
		if(judge.getJudgeA().length()!=0 &&judge.getJudgeB().length()!=0 &&judge.getJudgeContent().length()!=0 &&judge.getJudgeAnswer().length()!=0)
		{
			
			judgeService.save(judge);
			addActionMessage("添加成功!欢迎!");
			ActionContext.getContext().put("judges", judgeService.list());
			return "AddSucceed";
		}
				
		addActionError("题目内容、选项、答案不能为空!");
		return "AddFailed";
	}

	public String DeleteJudgeA(){
		ActionContext.getContext().put("deleteJudge", judgeService.findByID(judge.getJudgeID()));
		return "DeleteJudge";
	}
	
	public String DeleteJudgeB(){
		if(judge!=null)
			judgeService.delete(judge.getJudgeID());
		ActionContext.getContext().put("judges", judgeService.list());
		return "DeleteSucceed";
	}
	
}
